<?php
class ControllerLocalisationTaxClass extends AdminController {

	public function index() {
		M('localisation/tax_class');
		$this->getList();
	}

	public function insert() {
		M('localisation/tax_class');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_tax_class->addTaxClass($this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/tax_class'));
		}

		$this->getForm();
	}

	public function update() {
		M('localisation/tax_class');

		if ($this->request->isPost() && $this->validateForm()) {
			$this->model_localisation_tax_class->editTaxClass($this->request->get['tax_class_id'], $this->request->post);

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/tax_class'));
		}

		$this->getForm();
	}

	public function delete() {
		M('localisation/tax_class');

		if (isset($this->request->post['selected']) && $this->validateDelete()) {
			foreach ($this->request->post['selected'] as $tax_class_id) {
				$this->model_localisation_tax_class->deleteTaxClass($tax_class_id);
			}

			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('localisation/tax_class'));
		}

		$this->getList();
	}

	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);

		$qf = new Query_filter();

		$tax_class_total = $this->model_localisation_tax_class->getTotalTaxClasses();
		$this->data['tax_classes'] = $this->model_localisation_tax_class->getTaxClasses($qf);

		$this->data['pagination'] = $this->pagination($tax_class_total)->render();

		$this->display('localisation/tax_class_list.tpl');
	}

	protected function getForm() {
		$this->document->setTitle($this->_['heading_title']);

		if (isset($this->request->get['tax_class_id'])) {
			$this->data['action'] = UA('localisation/tax_class/update', 'tax_class_id=' . $this->request->get['tax_class_id']);
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_localisation_tax_class->getTaxClass($this->request->get['tax_class_id']);
			}
		} else {
			$this->data['action'] = UA('localisation/tax_class/insert');
		}
		
		M('localisation/tax_rate');

		$this->data['tax_rates'] = $this->model_localisation_tax_rate->getTaxRates();

		if (isset($this->request->post['tax_rule'])) {
			$this->data['tax_rules'] = $this->request->post['tax_rule'];
		} elseif (isset($this->request->get['tax_class_id'])) {
			$this->data['tax_rules'] = $this->model_localisation_tax_class->getTaxRules($this->request->get['tax_class_id']);
		} else {
			$this->data['tax_rules'] = array();
		}

		$this->display('localisation/tax_class_form.tpl');
	}

	protected function validateForm() {

		$pass = true;

		if (!range_length($this->request->post['title'], 3, 32)) {
			$this->setMessage('error_title', $this->_['error_title']);
			$pass = false;
		}

		if (!range_length($this->request->post['description'], 3, 255)) {
			$this->setMessage('error_description', $this->_['error_description']);
			$pass = false;
		}

		return $pass;
	}

	protected function validateDelete() {

		M('catalog/product');

		foreach ($this->request->post['selected'] as $tax_class_id) {
			$product_total = $this->model_catalog_product->getTotalProductsByTaxClassId($tax_class_id);

			if ($product_total) {
				$this->setMessage('error_warning', sprintf($this->_['error_product'], $product_total));
				return false;
			}
		}

		return true;
	}
}
?>